package com.software.loginregister.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.software.loginregister.R;
import com.software.loginregister.entity.RegionalFood;

import java.util.List;

public class RegionalFoodAdapter extends RecyclerView.Adapter<RegionalFoodAdapter.RegionalFoodViewHolder> {
    private List<RegionalFood> foodList;
    private OnItemClickListener listener;

    public RegionalFoodAdapter(List<RegionalFood> foodList) {
        this.foodList = foodList;
    }

    @NonNull
    @Override
    public RegionalFoodViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_regional_food, parent, false);
        return new RegionalFoodViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RegionalFoodViewHolder holder, int position) {
        RegionalFood food = foodList.get(position);

        // 设置食物图片
        holder.ivRegionalFood.setImageResource(food.getImageRes());

        // 设置食物名称
        holder.tvRegionalFoodName.setText(food.getName());

        // 设置食物描述
        holder.tvRegionalFoodDesc.setText(food.getDescription());

        // 设置推荐指数
        holder.tvRegionalRating.setText(String.format("推荐指数 %.1f", food.getRating()));

        // 设置点击事件
        holder.itemView.setOnClickListener(v -> {
            if (listener != null) {
                listener.onItemClick(food);
            }
        });
    }

    @Override
    public int getItemCount() {
        return foodList != null ? foodList.size() : 0;
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    public interface OnItemClickListener {
        void onItemClick(RegionalFood food);
    }

    static class RegionalFoodViewHolder extends RecyclerView.ViewHolder {
        ImageView ivRegionalFood;
        TextView tvRegionalFoodName;
        TextView tvRegionalFoodDesc;
        TextView tvRegionalRating;

        public RegionalFoodViewHolder(@NonNull View itemView) {
            super(itemView);
            ivRegionalFood = itemView.findViewById(R.id.iv_regional_food);
            tvRegionalFoodName = itemView.findViewById(R.id.tv_regional_food_name);
            tvRegionalFoodDesc = itemView.findViewById(R.id.tv_regional_food_desc);
            tvRegionalRating = itemView.findViewById(R.id.tv_regional_rating);
        }
    }
}